import sys
import random
import pygame
from pygame.math import Vector2

# 屏幕颜色
screen_color = (0, 0, 0)
# 单元格大小
cell_size = 20
cell_num = 20

pygame.init()
# 标题
pygame.display.set_caption("贪吃蛇")
# 屏幕大小
screen = pygame.display.set_mode((cell_num * cell_size, cell_num * cell_size))
clock = pygame.time.Clock()

USER_KEY_EVENT = pygame.USEREVENT
# 设置事件轮训时间
pygame.time.set_timer(USER_KEY_EVENT, 1000)

fruit_color = (126, 166, 114)  # 水果颜色


class Fruit:
    """水果"""

    def __init__(self):
        self.randomize()

    def randomize(self):
        """随机位置"""
        self.x = random.randint(1, cell_num - 2)
        self.y = random.randint(1, cell_num - 2)
        # self.color = (255, 0, 0)
        self.pos = Vector2(self.x, self.y)
        self.random_color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
        self.random_size = random.randrange(cell_size, 3* cell_size, cell_size)

    def draw(self):
        """在图上绘制水果"""
        fruit_rect = pygame.Rect(self.x * cell_size, self.y * cell_size, self.random_size, self.random_size)
        pygame.draw.rect(screen, self.random_color, fruit_rect)


snake_color = (183, 111, 122)  # 蛇体颜色


class Main:
    """主体类"""

    def __init__(self):
        """初始化 蛇、水果"""
        self.fruit = Fruit()

    def draw_elements(self):
        """绘制元素"""
        self.fruit.draw()

    def update_draw(self):
        """更新画面"""
        self.fruit.randomize()
        print(self.fruit.random_size)


def game_over():
    """游戏结束"""
    pygame.quit()
    sys.exit()


main = Main()

while True:
    """循环游戏事件"""
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            game_over()
        if event.type == USER_KEY_EVENT:
            main.update_draw()

    # 屏幕颜色
    main.draw_elements()
    pygame.display.update()
    screen.fill(screen_color)
    clock.tick(5000)
